os_pipe.rs
A cross-platform library for opening OS pipes.
The standard library uses pipes to read output from child processes,
but it doesn't expose a way to create them directly. This crate
fills that gap with the pipe
function. It also includes some
helpers for passing pipes to the std::process::Command
API.
Usage note: The main purpose of os_pipe
is to support the
higher-level duct
library, which handles most of the same use cases with much less
code and no risk of deadlocks. duct
can run the entire example
below in one line of code.
Changes
- 0.8.0
- Remove the
From<...> for File
impls. While treating a pipe or a tty as a file works pretty smoothly on Unix, it's questionable on Windows. For example,File::metadata
may return an error, or it might succeed but then incorrectly returntrue
fromis_file
. Now that the standard library'sStdin
/Stdout
/Stderr
types all implementAsRawFd
/AsRawHandle
, callers who know what they're doing can use those interfaces, rather than relying onos_pipe
.
- Remove the
- 0.7.0
- Implement
From<PipeReader>
andFrom<PipeWriter>
forStdio
andFile
. The latter is useful for APIs that require aFile
, likememmap
, together withdup_stdin
etc. below. - Remove the
IntoStdio
trait. Since Rust 1.20,PipeReader
andPipeWriter
(as well as the standardFile
) can be passed directly tostd::process::Command
, without any extra conversion. - Replace
parent_stdin
/parent_stdout
/parent_stderr
withdup_stdin
/dup_stdout
/dup_stderr
, which returnPipeReader
orPipeWriter
instead ofStdio.
- Implement
Example
Join the stdout and stderr of a child process into a single stream, and read it. To do that we open a pipe, duplicate its write end, and pass those writers as the child's stdout and stderr. Then we can read combined output from the read end of the pipe. We have to be careful to close the write ends first though, or reading will block waiting for EOF.
use pipe;
use *;
use ;
// This command prints "foo" to stdout and "bar" to stderr. It
// works on both Unix and Windows, though there are whitespace
// differences that we'll account for at the bottom.
let shell_command = "echo foo && echo bar >&2";
// Ritual magic to run shell commands on different platforms.
let = if cfg! else ;
let mut child = new;
child.arg;
child.arg;
// Here's the interesting part. Open a pipe, copy its write end, and
// give both copies to the child.
let = pipe.unwrap;
let writer_clone = writer.try_clone.unwrap;
child.stdout;
child.stderr;
// Now start the child running.
let mut handle = child.spawn.unwrap;
// Very important when using pipes: This parent process is still
// holding its copies of the write ends, and we have to close them
// before we read, otherwise the read end will never report EOF. The
// Command object owns the writers now, and dropping it closes them.
drop;
// Finally we can read all the output and clean up the child.
let mut output = String new;
reader.read_to_string.unwrap;
handle.wait.unwrap;
assert!;